package com.situ.hospital.dao.Impl;

import com.situ.hospital.dao.IDepartmentDAO;
import com.situ.hospital.pojo.Department;
import com.situ.hospital.pojo.query.DepartmentQuery;
import com.situ.hospital.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DepartmentDAOImpl implements IDepartmentDAO {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Department> selectAll() {
        String sql = "SELECT id,`name`,address,create_time,update_time FROM department";
        return template.query(sql, new BeanPropertyRowMapper<>(Department.class));
    }

    @Override
    public List<Department> selectByPage(DepartmentQuery departmentQuery) {
        String sql = "SELECT id,name,address,create_time,update_time FROM department WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = departmentQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Date beginCreateTime = departmentQuery.getBeginCreateTime();
        Date endCreateTime = departmentQuery.getEndCreateTime();
        if(beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql += " LIMIT ?,?";
        int offset = (departmentQuery.getPage() - 1) * departmentQuery.getLimit();
        queryList.add(offset);
        queryList.add(departmentQuery.getLimit());
        return template.query(sql, new BeanPropertyRowMapper<>(Department.class), queryList.toArray());
    }

    @Override
    public int selectTotalCount(DepartmentQuery departmentQuery) {
        String sql = "SELECT COUNT(*) FROM department WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = departmentQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Date beginCreateTime = departmentQuery.getBeginCreateTime();
        Date endCreateTime = departmentQuery.getEndCreateTime();
        if(beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        String sql = "DELETE FROM department WHERE id=?";
        template.update(sql, id);
    }

    @Override
    public void add(Department department) {
        String sql = "INSERT INTO department(name,address) VALUES(?,?)";
        template.update(sql, department.getName(), department.getAddress());
    }

    @Override
    public Department selectById(int id) {
        String sql = "SELECT id,name,address,create_time,update_time FROM department WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Department.class), id);
    }

    @Override
    public void update(Department department) {
        String sql = "UPDATE department SET name=?,address=? WHERE id=?";
        template.update(sql, department.getName(), department.getAddress(), department.getId());
    }
}
